app.controller('GetAuthlistCtrl',function ($scope,$state,$http,$location,$window,$localStorage) {
	$scope.namedata = $localStorage.cur_user.uname;
	$http.post(
        $scope.app.backendurl+"admin-role/get-rights",
        {
            'id': $state.params.roleid,
            'token':$localStorage.token
        }
    ).success(function (data) {
      console.log(data);
      if(data.code==0&&data.msg==""){
       var defaultData = parseNodes(data.data.rights) ;
       console.log(defaultData);
       $("#treeview").kendoTreeView({
                    checkboxes: {
                        checkChildren: true
                    },
                    check: onCheck,

                    dataSource:defaultData
        });

       	    function checkedNodeIds(nodes, checkedNodes) {
       	    	//console.log(checkedNodes);
                    for (var i = 0; i < nodes.length; i++) {
                        if (nodes[i].checked) {
                            if(nodes.parent()!= undefined){
                                if(nodes.parent().parent()!=undefined){
                                    if(nodes.parent().parent().parent()!=undefined){
                                        checkedNodes.push(nodes[i].rid);
                                        checkedNodes.push(nodes.parent().parent().parent().rid);
                                    }  
                                    checkedNodes.push(nodes[i].fid);
                                    checkedNodes.push(nodes.parent().parent().fid);
                                }
                                checkedNodes.push(nodes[i].mid);
                                checkedNodes.push(nodes.parent().mid);
                            }
                        }
                        if (nodes[i].hasChildren) {
                            checkedNodeIds(nodes[i].children.view(), checkedNodes);
                        }
                    }
            }
            function onCheck() {
                var checkedNodes = [],
                    treeView = $("#treeview").data("kendoTreeView"),
                    message;

                checkedNodeIds(treeView.dataSource.view(), checkedNodes);

                if (checkedNodes.length > 0) {                	
                    message =  checkedNodes.join(",");
                    
                } else {
                    message = "";
                }

                return message;
            }

                $("#savecompetence").on('click', function () {
                    var str = onCheck();
                    //console.log(str);
                    var strArr = str.split(",");
                    strArr.sort();
                    var result = new Array();
                    var tempStr = "";
                    for(var i in strArr){
                        if(strArr[i]!= tempStr){
                            result.push(parseInt(strArr[i]));
                            tempStr = strArr[i];
                        }else{
                            continue;
                        }                       
                    }
                   //console.log(result);
                    var rids = "["+result+"]";
                    $http.post(
                       $scope.app.backendurl+"admin-role/save-rights",
                        {
                            'id': $state.params.roleid,                            
                            'rids': rids,
                            'token':$localStorage.token
                        }
                    ).success(function (data) {
                        if(data.code==0&&data.msg=="保存成功"){
                            //console.log(data);
                            alert(data.msg);
                            $window.location.reload('app.sys.roleauth')
                           
                           //$state.go('app.sys.role');
                        }
                           
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })
                });



      }
       if(data.code==20001){
         $location.path('access.signin');
       }
    }).error(function (data) {
        alert(data);
    })


})

function parseNodes (nodes){
    var ns = [];
    //var all_checked = true;
    for(var i = 0; i< nodes.length; i++){
        var node = nodes[i];
        var nd = {};
        if(node['mid']!=null||node['mid']!=undefined){
        	nd.mid = node['mid'];
        	nd.id = nd.mid;
        	
        }
        if(node['fid']!=null||node['fid']!=undefined){
        	nd.fid = node['fid'];
        	nd.id = nd.fid;
        	
        }
        if(node['rid']!=null||node['rid']!=undefined){
        	nd.rid = node['rid'];
        	nd.id = nd.rid;
        }
        nd.text = node['text'];     
        if(node['nodes'] != null || node['nodes'] != undefined){
            var next = parseNodes(node['nodes']);
            if(next.length > 0){
                nd.items = next;
                nd.expanded = true;
                nd.checked = node.checked;
            }      
        }
        ns[i] = nd;  
    }
    return ns;
}